var session = sessionStorage.getItem("session");
if (!session) {
	window.parent.location = "../../login.html";
}
$(document).ready(function() {
	$("#tt").tree({
		url: IP + "GetMySystemMenu?callback=?&session=" + session + "&rid=0",
	})
	$("#jqGrid").jqGrid({
		url: IP + "GetSevRoles?callback=?&session=" + session,
		mtype: "Get",
		datatype: "jsonp",
		colModel: [{
			label: '编号',
			sorttype: 'integer',
			name: 'rid',
			align: "center",
			key: true,
			width: 120,
			hidden: true,
		}, {
			label: '角色名',
			name: 'rname',
			width: 200
		}],
		loadonce: true,
		onSelectRow: function(rowid) {
			$("#rname").textbox("disable");
			$.ajax({
				type: "get",
				url: IP + "GetRolesNemuID?&session=" + session + "&rid=" + rowid,
				dataType: 'jsonp',
				jsonp: "callback",
				jsonpCallback: "resultHandler",
				success: function(data) {
					var nodes = $('#tt').tree('getChecked');
					for (var j = 0; j < nodes.length; j++) {
						$("#tt").tree("uncheck", nodes[j].target);
					}
					for (var i = 0; i < data.length; i++) {
						var treeNode = $('#tt').tree("find", data[i]);
						// console.log(treeNode);
						var cc = $("#tt").tree("getChildren", treeNode.target);
						console.log(cc);
						if (cc.length == 0) {
							$("#tt").tree("check", treeNode.target);
						}
					}
				}
			});
		},
		viewrecords: true,
		rowNum: 15,
		autowidth: true,
		page: 1,
		loadtext: '正在加载数据',
		rownumbers: true,
		pager: "#jqGridPager"
	});
	var h = $("body").height();
	$("#jqGrid").jqGrid("setGridHeight", h - 200)
	var template1 = {
		"groupOp": "AND",
		"rules": [{
			"field": "rname",
			"op": "eq",
			"data": "W"
		}]
	};
	// activate the build in search with multiple option
	$('#jqGrid')
		.navGrid("#jqGridPager", {
				search: true, // show search button on the toolbar
				add: false,
				edit: false,
				del: false,
				refresh: true
			}, {}, // edit options
			{}, // add options
			{}, // delete options
			{
				multipleSearch: true,
				multipleGroup: true,
				showQuery: true,
				// set the names of the template
				tmplNames: ["模板一"],
				// set the template contents
				tmplFilters: [template1]
			} // search options - define multiple search
		);
	AddRow();
});

function selectRow() {
	jQuery('#jqGrid').jqGrid('setSelection', '2');
}

function AddRow() {
	$("#rname").textbox("enable");
	$('#ff').form('load', {
		rid: 0,
		rname: '',
	}); 
	$("#tt").tree({
		url: IP + "GetMySystemMenu?callback=?&session=" + session + "&rid=0",
	});
	$("#jqGrid").trigger("reloadGrid");
}

function EditRow() {
	$("#rname").textbox("enable");
	var grid = $("#jqGrid");
	var rowKey = grid.jqGrid('getGridParam', "selrow");
	if (rowKey) {
		$('#ff').form('load', {
			rid: $("#jqGrid").getCell(rowKey, "rid"),
			rname: $("#jqGrid").getCell(rowKey, "rname"),
		});
	} else {
		$.messager.alert('提醒', "没有选中角色", 'info');
	}
}

function getCheckedId() {
	var nodes = $('#tt').tree('getChecked');
	var lastId;
	var s = [];
	for (var i = 0; i < nodes.length; i++) {
		s.push(nodes[i].id);
		FID = $("#tt").tree("getParent", nodes[i].target).id;
		if (FID != lastId) {
			s.push(FID);
			lastId = FID;
		}
	}
	return s.join(",");
}

function SaveRow() {
	var grid = $("#jqGrid");
	var rowKey = grid.jqGrid('getGridParam', "selrow");
	var oldvalue = $("#jqGrid").getCell(rowKey, "rname");
	if ($('#ff').form('enableValidation').form('validate')) {
		var ids = getCheckedId();

		if (ids == "") {
			$.messager.alert("提醒", "请选择相应权限", 'info');
			return;
		}
		$.ajax({
			type: "post", // 与可以 "get"
			url: IP + "UpdateSrvRoles?callback=?&session=" + session,
			data: {
				ridId: $('#rid').val(),
				rname: $('#rname').val(),
				savestr: ids,
			},
			dataType: "jsonp",
			jsonp: "callback",
			jsonpCallback: "resultHandler",
			success: function(json) {
				//console.log(json);
				if (json.result = 200) {
					if ($('#rid').val() == 0) {
						$("#jqGrid").addRowData(json.id, {
							rid: json.id,
							rname: $("#rname").val(),
						}, '');
						insertopertlog('系统管理', '角色权限管理', '新增', '', $('#rname').val());
						$.messager.show({
							title: '提醒',
							msg: '角色权限增加成功',
							timeout: 1300,
							showType: 'slide',
							style: {
								right: '',
								bottom: ''
							}
						});
					} else {
						$("#jqGrid").setRowData($('#rid').val(), {
							rid: $("#rid").val(),
							rname: $("#rname").val(),
						}, '');
						insertopertlog('系统管理', '角色权限管理', '修改', oldvalue, $('#rname').val());
						$.messager.show({
							title: '提醒',
							msg: '角色权限修改成功',
							timeout: 1300,
							showType: 'slide',
							style: {
								right: '',
								bottom: ''
							}
						});
					}
				} else {
					$.messager.alert('保存失败', json.message, 'error');
				}
				//保存成功后，重置控件，以免用户重复提交
				AddRow();
			}
		});
	}
}

function DeleteRow() {
	var grid = $("#jqGrid");
	var rowKey = grid.jqGrid('getGridParam', "selrow");
	var oldvalue = $("#jqGrid").getCell(rowKey, "rname");
	if (rowKey) {
		$.messager.confirm('删除提醒', '你确认要删除角色' + $("#jqGrid").getCell(rowKey, "rname") + '?', function(r) {
			if (r) {
				$.ajax({
					type: "post", // 与可以 "get"
					url: IP + "DeleteSrvRoles?callback=?&session+" + session,
					data: {
						session: session,
						id: rowKey,
					},
					dataType: "jsonp",
					jsonp: "callback",
					jsonpCallback: "resultHandler",
					success: function(json) {
						if (json.result == 200) {
							$("#jqGrid").delRowData(rowKey);
							insertopertlog('系统管理', '角色权限管理', '删除', oldvalue, '');
							$.messager.show({
								title: '提醒',
								msg: '删除成功',
								timeout: 1300,
								showType: 'slide',
								style: {
									right: '',
									bottom: ''
								}
							});
						} else {
							$.messager.alert('删除失败', json.message, 'error');
						}
					}
				});
			}
		});
	} else {
		$.messager.alert('提醒', "没有选中行", 'info');
	}
	AddRow();
}